/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:23:00 AM
------------------------------------------------------*/
using hou_elearning.DataAccess.Base;
using hou_elearning.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;

namespace hou_elearning.DataAccess
{    
    public class ThongtinbaikiemtraDAL : SqlProvider<ThongtinbaikiemtraEntity>
    {
        static ThongtinbaikiemtraDAL()
        {
            InitReader();
        }
        protected static void InitReader()
        {
            getFromReader=delegate(SqlDataReader dr)
            {
                ThongtinbaikiemtraEntity entity = new ThongtinbaikiemtraEntity();
				entity.PK_iThongtinbaikiemtraID = Int64.Parse("0"+dr["PK_iThongtinbaikiemtraID"].ToString());
				entity.FK_iHocvienID = Int64.Parse("0"+dr["FK_iHocvienID"].ToString());
				entity.FK_iKiemtraID = Int64.Parse("0"+dr["FK_iKiemtraID"].ToString());
				entity.fDiem = float.Parse("0"+dr["fDiem"].ToString());
				entity.iTrangthai = Int16.Parse("0"+dr["iTrangthai"].ToString());
				entity.dThoigiannopbai =String.IsNullOrEmpty(dr["dThoigiannopbai"].ToString())?DateTime.Now:DateTime.Parse(dr["dThoigiannopbai"].ToString());
				entity.sNhanxet = dr["sNhanxet"].ToString();
				entity.FK_iGiangvienchambaiID = Int64.Parse("0"+dr["FK_iGiangvienchambaiID"].ToString());
                return entity;
            };
        }
        public static ThongtinbaikiemtraEntity GetOne(Int64 PK_iThongtinbaikiemtraID)
        {
            string cmdName = "spThongtinbaikiemtra_GetByPK";
            SqlParameter p = new SqlParameter("@PK_iThongtinbaikiemtraID", PK_iThongtinbaikiemtraID);
            ThongtinbaikiemtraEntity entity = GetOne(cmdName, p);
            return entity;
        }
        public static List<ThongtinbaikiemtraEntity> GetAll()
        {
            string cmdName = "spThongtinbaikiemtra_Get";
            return GetList(cmdName);
        }
        public static List<ThongtinbaikiemtraEntity> GetByFK_iHocvienID(Int64 FK_iHocvienID)
		{
			string cmdName = "spThongtinbaikiemtra_GetByFK_FK_iHocvienID";
			SqlParameter p = new SqlParameter("@FK_iHocvienID",FK_iHocvienID);
			List<ThongtinbaikiemtraEntity> list = GetList(cmdName, p);
			return list;
		}public static List<ThongtinbaikiemtraEntity> GetByFK_iKiemtraID(Int64 FK_iKiemtraID)
		{
			string cmdName = "spThongtinbaikiemtra_GetByFK_FK_iKiemtraID";
			SqlParameter p = new SqlParameter("@FK_iKiemtraID",FK_iKiemtraID);
			List<ThongtinbaikiemtraEntity> list = GetList(cmdName, p);
			return list;
		}public static List<ThongtinbaikiemtraEntity> GetByFK_iGiangvienchambaiID(Int64 FK_iGiangvienchambaiID)
		{
			string cmdName = "spThongtinbaikiemtra_GetByFK_FK_iGiangvienchambaiID";
			SqlParameter p = new SqlParameter("@FK_iGiangvienchambaiID",FK_iGiangvienchambaiID);
			List<ThongtinbaikiemtraEntity> list = GetList(cmdName, p);
			return list;
		}
        public static int GetByFK_iHocvienID_FK_iKiemtraID(ThongtinbaikiemtraEntity entity)
        {
            string cmdName = "spThongtinbaikiemtra_GetByFK_iHocvienID_FK_iKiemtraID";
            SqlParameter[] p = new SqlParameter[3];
            p[0] = new SqlParameter("@PK_iThongtinbaikiemtraID", entity.PK_iThongtinbaikiemtraID);
            p[1] = new SqlParameter("@FK_iHocvienID", entity.FK_iHocvienID);
            p[2] = new SqlParameter("@FK_iKiemtraID", entity.FK_iKiemtraID);
            return Run(cmdName, true, p);

        }
        public static int Add(ThongtinbaikiemtraEntity entity)
        {
            string cmdName = "spThongtinbaikiemtra_Insert";
            return Run(cmdName,true,initParams(entity));

        }
        public static int Add_non_FK_iGiangvienchambaiID(ThongtinbaikiemtraEntity entity)
        {
            string cmdName = "spThongtinbaikiemtra_Insert_non_FK_iGiangvienchambaiID";
            return Run(cmdName, true, initParams1(entity));

        }
        public static bool Update_non_FK_iGiangvienchambaiID(ThongtinbaikiemtraEntity entity)
        {
            string cmdName = "spThongtinbaikiemtra_Update_non_FK_iGiangvienchambaiID";
            return Run(cmdName, false, initParams1(entity)) > 0;
        }
        public static List<ThongtinbaikiemtraEntity> GetBy_FK_iKiemtraID_iTrangthai( Int64 FK_iKiemtraID, Int16 iTrangthai)
        {
            string cmdName = "spKiemtra_GetBy_FK_iKiemtraID_iTrangthai";
            SqlParameter[] p = new SqlParameter[2];           
            p[0] = new SqlParameter("@FK_iKiemtraID", FK_iKiemtraID);
            p[1] = new SqlParameter("@iTrangthai", iTrangthai);
            List<ThongtinbaikiemtraEntity> list = GetList(cmdName, p);
            return list;
        }
        public static bool Edit(ThongtinbaikiemtraEntity entity)
        {
            string cmdName = "spThongtinbaikiemtra_Update";
            return Run(cmdName,false,initParams(entity))>0;
        }

        public static bool Remove(Int64 PK_iThongtinbaikiemtraID)
        {
            string cmdName = "spThongtinbaikiemtra_Delete";
            SqlParameter p = new SqlParameter("@PK_iThongtinbaikiemtraID", PK_iThongtinbaikiemtraID);
            return Run(cmdName,false,p)>0;
        }
        
        #region private
        private static SqlParameter[] initParams(ThongtinbaikiemtraEntity entity)
        {
            SqlParameter[] p = new SqlParameter[8];
			p[0] = new SqlParameter("@PK_iThongtinbaikiemtraID", entity.PK_iThongtinbaikiemtraID);
			p[1] = new SqlParameter("@FK_iHocvienID", entity.FK_iHocvienID);
			p[2] = new SqlParameter("@FK_iKiemtraID", entity.FK_iKiemtraID);
			p[3] = new SqlParameter("@fDiem", entity.fDiem);
			p[4] = new SqlParameter("@iTrangthai", entity.iTrangthai);
			p[5] = new SqlParameter("@dThoigiannopbai", entity.dThoigiannopbai);
			p[6] = new SqlParameter("@sNhanxet", entity.sNhanxet);
			p[7] = new SqlParameter("@FK_iGiangvienchambaiID", entity.FK_iGiangvienchambaiID);
            return p;
        }
        private static SqlParameter[] initParams1(ThongtinbaikiemtraEntity entity)
        {
            SqlParameter[] p = new SqlParameter[7];
            p[0] = new SqlParameter("@PK_iThongtinbaikiemtraID", entity.PK_iThongtinbaikiemtraID);
            p[1] = new SqlParameter("@FK_iHocvienID", entity.FK_iHocvienID);
            p[2] = new SqlParameter("@FK_iKiemtraID", entity.FK_iKiemtraID);
            p[3] = new SqlParameter("@fDiem", entity.fDiem);
            p[4] = new SqlParameter("@iTrangthai", entity.iTrangthai);
            p[5] = new SqlParameter("@dThoigiannopbai", entity.dThoigiannopbai);
            p[6] = new SqlParameter("@sNhanxet", entity.sNhanxet);
          
            return p;
        }
        #endregion
       
    }
}